package com.dunqi.gpm.shunqing.home.fragment;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.FrameLayout;

import com.dunqi.gpm.shunqing.R;
import com.dunqi.gpm.shunqing.common.api.ApiFactory;
import com.dunqi.gpm.shunqing.common.api.RxSchedulers;
import com.dunqi.gpm.shunqing.common.base.BaseFragment;
import com.dunqi.gpm.shunqing.common.util.CommonUtil;
import com.dunqi.gpm.shunqing.home.adapter.TreeHolder;
import com.dunqi.gpm.shunqing.home.bean.tree.AdditionalParameters;
import com.dunqi.gpm.shunqing.home.bean.tree.Data;
import com.dunqi.gpm.shunqing.home.bean.tree.ParentAdditionalParameters;
import com.dunqi.gpm.shunqing.home.bean.tree.ProTree;
import com.dunqi.gpm.shunqing.home.constance.Constance;
import com.dunqi.gpm.shunqing.home.eventbus.TreeViewDelete;
import com.unnamed.b.atv.model.TreeNode;
import com.unnamed.b.atv.view.AndroidTreeView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.LinkedHashMap;
import java.util.Map;

import butterknife.BindView;
import io.reactivex.functions.Consumer;

/**
 * Created by 空 on 2017/11/24 0024.
 */

public class WorkInstructionsFrgament extends BaseFragment {

    @BindView(R.id.container)
    FrameLayout container;

    private Long mainObjectId;
    private boolean read_only;
    private Long instructionType;

//    String json = "{\n" +
//            "    \"data\": {\n" +
//            "        \"3\": {\n" +
//            "            \"additionalParameters\": {\n" +
//            "                \"children\": {\n" +
//            "                    \"4\": {\n" +
//            "                        \"additionalParameters\": {\n" +
//            "                            \"children\": null,\n" +
//            "                            \"code\": \"15\",\n" +
//            "                            \"data\": {\n" +
//            "                                \"instructionText\": \"感谢领导关怀\",\n" +
//            "                                \"instructionType\": 1,\n" +
//            "                                \"instructorId\": 1049,\n" +
//            "                                \"instructorOrgId\": 1019,\n" +
//            "                                \"instructTime\": \"2017-12-10 12:56:21\",\n" +
//            "                                \"isLeaf\": 0,\n" +
//            "                                \"levels\": 2,\n" +
//            "                                \"msgType\": 3,\n" +
//            "                                \"paths\": \",3\",\n" +
//            "                                \"projectId\": 15,\n" +
//            "                                \"projectInstructionId\": 4,\n" +
//            "                                \"replyProInstructionId\": 3,\n" +
//            "                                \"sendSmsNotice\": 0,\n" +
//            "                                \"userName\": \"程智\"\n" +
//            "                            },\n" +
//            "                            \"id\": \"4\",\n" +
//            "                            \"item-selected\": false,\n" +
//            "                            \"name\": \"程智\"\n" +
//            "                        },\n" +
//            "                        \"homeMenuId\": \"folder\"\n" +
//            "                    }\n" +
//            "                },\n" +
//            "                \"code\": \"15\",\n" +
//            "                \"data\": {\n" +
//            "                    \"instructionText\": \"请注意施工安全\",\n" +
//            "                    \"instructionType\": 1,\n" +
//            "                    \"instructorId\": 1045,\n" +
//            "                    \"instructorOrgId\": 1014,\n" +
//            "                    \"instructTime\": \"2017-12-10 12:36:49\",\n" +
//            "                    \"isLeaf\": 0,\n" +
//            "                    \"levels\": 1,\n" +
//            "                    \"msgType\": 2,\n" +
//            "                    \"paths\": \"\",\n" +
//            "                    \"projectId\": 15,\n" +
//            "                    \"projectInstructionId\": 3,\n" +
//            "                    \"replyProInstructionId\": null,\n" +
//            "                    \"sendSmsNotice\": 0,\n" +
//            "                    \"userName\": \"陈旭东\"\n" +
//            "                },\n" +
//            "                \"id\": \"3\",\n" +
//            "                \"item-selected\": false,\n" +
//            "                \"name\": \"陈旭东\"\n" +
//            "            },\n" +
//            "            \"homeMenuId\": \"folder\"\n" +
//            "        }\n" +
//            "    }\n" +
//            "}";

    @Override
    protected void refreshData(Bundle savedInstanceState) {
        net();
    }

    private void net() {
        final ProgressDialog dialog = ProgressDialog.show(context, "提示", "正在拉取数据...");
        ApiFactory.getInstance()
                .getProTree(mainObjectId, instructionType, read_only)
                .compose(RxSchedulers.<ProTree>io_main())
                .subscribe(new Consumer<ProTree>() {
                    @Override
                    public void accept(ProTree proTree) throws Exception {
                        initTree(proTree);
                        dialog.dismiss();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        dialog.dismiss();
                        CommonUtil.showSingleToast("获取数据失败");
                    }
                });


    }

    private void initTree(ProTree proTree) {
        TreeNode root = TreeNode.root();

        parse(proTree.getData(), root);
        root.setExpanded(true);

        AndroidTreeView tView = new AndroidTreeView(getActivity(), root);
        tView.setUse2dScroll(true);

        container.removeAllViews();
        container.addView(tView.getView());
    }

    private void parse(LinkedHashMap<String, ParentAdditionalParameters> children, TreeNode parentNode) {
        if (children == null) {
            return;
        }
        for (Map.Entry<String, ParentAdditionalParameters> entry : children.entrySet()) {
            ParentAdditionalParameters value = entry.getValue();
            AdditionalParameters parameters = value.getAdditionalParameters();
            Data parametersData = parameters.getData();
            TreeNode treeNodeChild = new TreeNode(parametersData)
                    .setViewHolder(new TreeHolder(getActivity(), mainObjectId, instructionType));

            parse(parameters.getChildren(), treeNodeChild);

            treeNodeChild.setExpanded(true);
            parentNode.addChild(treeNodeChild);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 200) {
//            InstructionsActivity
            net();
        }
    }

    @Override
    protected void initUiAndListener() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_work_instruction;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        Bundle arguments = getArguments();
        mainObjectId = arguments.getLong(Constance.MAIN_OBJECT_ID);
        instructionType = arguments.getLong(Constance.INSTRUCTION_TYPE);
        read_only = arguments.getBoolean(Constance.READ_ONLY);
    }

    public static WorkInstructionsFrgament newInstance(Long mainObjectId, Long instructionType, boolean read_only) {
        WorkInstructionsFrgament frgament = new WorkInstructionsFrgament();
        Bundle bundle = new Bundle();
        if (mainObjectId != null) {
            bundle.putLong(Constance.MAIN_OBJECT_ID, mainObjectId);
        }
        if (instructionType != null) {
            bundle.putLong(Constance.INSTRUCTION_TYPE, instructionType);
        }
        bundle.putBoolean(Constance.READ_ONLY, read_only);
        frgament.setArguments(bundle);
        return frgament;
    }

    @Subscribe
    public void deleteAndRefreshUi(TreeViewDelete delete) {
        net();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
